/*
 * SoundButton.java
 * Copyright (C) 2007 Ezequiel Turovetzky
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
package ar.com.botonera.gui;

import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import javazoom.jl.player.Player;
import ar.com.botonera.NoSuchMp3Exception;
import ar.com.botonera.gui.common.ImageLoader;
import ar.com.botonera.gui.common.listeners.ShowContextualMenuAdapter;
import ar.com.botonera.utils.ExceptionHandlerFactory;

/**
 * Las instancias de esta clase son los botones de la botonera.
 * <br>Al ser pulsado lanza un thread encargado de
 * reproducir el archivo <code>.mp3</code> que se le asoci&oacute; a la
 * instancia en el momento de la creaci&oacute;n
 * a este bot&oacute;n.
 * @author <a href="mailto:tezequiel@gmail.com">Ezequiel Turovetzky</a>
 * @version 1.0
 */
public class SoundButton extends JButton implements ActionListener {

    //~ Static fields/initializers ---------------------------------------------

    /** Icono del boton en estado normal */
    public static final Icon NORMAL_ICON = ImageLoader.loadIcon("images/boton.png");
    /** Icono del boton al ser pulsado */
    public static final Icon PRESSED_ICON = ImageLoader.loadIcon("images/boton_pressed.png");
    
    //~ Instance fields --------------------------------------------------------

    /** El archivo a reproducir */
    private File mp3;

    //~ Constructors -----------------------------------------------------------

    /**
     * Crea una nueva instancia de esta clase.
     * @param value Texto del bot&oacute;n.
     * @param mp3 Archivo mp3 a reproducir.
     * @throws NoSuchMp3Exception Si el archivo <code>mp3</code> no existe.
     */
    public SoundButton(String value, File mp3) {
        super(value, NORMAL_ICON);

        if((mp3 == null) || !mp3.exists()) {
        	ExceptionHandlerFactory.getExceptionHandler().handle(new NoSuchMp3Exception(mp3));
        }

        this.mp3 = mp3;
        setPressedIcon(PRESSED_ICON);
        setFocusPainted(false);
        setBorderPainted(false);
        setContentAreaFilled(false);
        setVerticalTextPosition(SwingConstants.BOTTOM);
        setHorizontalTextPosition(SwingConstants.CENTER);
        setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));
        setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        addActionListener(this);
        addMouseListener(new ShowContextualMenuAdapter());
    }

    //~ Methods ----------------------------------------------------------------

    /**
     * Devuelve el archivo <code>mp3</code> que reproduce este bot&oacute;n.
     * @return El archivo <code>mp3</code> que reproduce este bot&oacute;n.
     */
    public File getMp3() {
        return mp3;
    }
    
    @Override
    public String toString() {
    	return "[" + getText() + "|" + getMp3() + "]"; 
    }

    /**
     * Lanza un thread que reproduce el archivo <code>mp3</code> asociado
     * a esta bot&oacute;n.
     */
    @Override
    public void actionPerformed(ActionEvent e) {

        Thread t = new Thread(new Runnable() {
            public void run() {

                try {

                    URL url = mp3.toURI().toURL();
                    Player player = new Player(url.openStream());
                    player.play();
                    
                } catch(Exception e) {
                	ExceptionHandlerFactory.getExceptionHandler().handle(e);
                }
            }
        });

        t.start();
    }
}
